package com.xiaolin.oa.controller;

import com.github.pagehelper.PageInfo;
import com.xiaolin.oa.annotations.HasApi;
import com.xiaolin.oa.constants.ApiPermConstant;
import com.xiaolin.oa.model.dto.HandleProcessDto;
import com.xiaolin.oa.model.dto.ProcessNodeDetail;
import com.xiaolin.oa.model.dto.StartProcessDto;
import com.xiaolin.oa.model.dto.SysUserView;
import com.xiaolin.oa.model.entity.CommonProcess;
import com.xiaolin.oa.service.CommonProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolin
 * @description 描述
 * @since 2024/7/12
 */
@Api("审批流程资源")
@RestController
@RequestMapping("/process")
public class CommonProcessController {

    @Autowired
    private CommonProcessService processService;

    @HasApi({ApiPermConstant.COMMON_PERM})
    @ApiOperation("获取所有审批流程")
    @PostMapping("/getList")
    public List<CommonProcess> getProcessList(@RequestBody CommonProcess process) {
        return processService.getProcessList(process);
    }

    @HasApi({"process:home"})
    @ApiOperation("获取审批流程列表")
    @PostMapping("/getPage/{page}/{pageSize}")
    public PageInfo<CommonProcess> getProcessPage(@PathVariable Integer page,
                                                  @PathVariable Integer pageSize,
                                                  @RequestBody CommonProcess process) {
        return processService.getProcessPage(page, pageSize, process);
    }

    @HasApi({"process:add"})
    @ApiOperation("添加审批流程")
    @PostMapping("/add")
    public boolean addProcess(@RequestBody CommonProcess process) {
        return processService.addProcess(process);
    }

    @HasApi({"process:get"})
    @ApiOperation("获取审批流程")
    @PostMapping("/get/{id}")
    public CommonProcess getProcess(@PathVariable("id") Integer id) {
        return processService.getProcess(id);
    }

    @HasApi({"process:update"})
    @ApiOperation("修改审批流程")
    @PostMapping("/update")
    public boolean updateProcess(@RequestBody CommonProcess process) {
        return processService.updateProcess(process);
    }

    @HasApi({"process:enable"})
    @ApiOperation("启用禁用")
    @PostMapping("/status/{id}/{status}")
    public boolean status(@PathVariable Integer id, @PathVariable String status) {
        return processService.status(id, status);
    }

    @HasApi({"process:binding:org"})
    @ApiOperation("流程绑定执行人组织架构")
    @PostMapping("/configOrgForProcess/{processId}")
    public boolean configOrgForProcess(@PathVariable Integer processId, @RequestBody List<Integer> orgIds) {
        return processService.configOrgForProcess(processId, orgIds);
    }

    @HasApi({"process:bound:orgs"})
    @ApiOperation("获取绑定的执行人组织架构")
    @PostMapping("/getBoundOrgs/{processId}")
    public List<Integer> getBoundOrgs(@PathVariable Integer processId) {
        return processService.getBoundOrgs(processId);
    }

    @ApiOperation("获取执行人组织架构下的用户")
    @PostMapping("/getExecutorOrgUserList/{processId}")
    public List<SysUserView> getExecutorOrgUserList(@PathVariable Integer processId) {
        return processService.getExecutorOrgUserList(processId);
    }

    @ApiOperation("获取流程首节点")
    @PostMapping("/getFirstProcessNode/{processCode}")
    public ProcessNodeDetail getFirstProcessNode(@PathVariable String processCode) {
        return processService.getFirstProcessNode(processCode);
    }

    @ApiOperation("获取流程下一个节点")
    @PostMapping("/getNextProcessNode/{serialNumber}/{processFlowId}")
    public ProcessNodeDetail getNextProcessNode(@PathVariable String serialNumber,
                                                @PathVariable Integer processFlowId) {
        return processService.getNextProcessNode(serialNumber, processFlowId);
    }

    @ApiOperation("发起流程")
    @PostMapping("/startProcess")
    public void startProcess(@RequestBody StartProcessDto startProcessDto) {
        processService.startProcess(startProcessDto);
    }

    @ApiOperation("处理流程")
    @PostMapping("/handleProcess")
    public String handleProcess(@RequestBody HandleProcessDto handleProcessDto) {
        return processService.handleProcess(handleProcessDto);
    }

}
